import { getOssConfig } from '@/api/component/oss/oss'
import defaultSettings from '@/settings'
import { setOssConfig } from '@/utils/upload-oss'

const { ossFileRoot } = defaultSettings

const ossconfig = {
  state: {
    ossFileRoot: ossFileRoot,
    endPonint: '',
    prefix: '',
    bucket: '',
    region: ''
  },

  mutations: {
    SET_CONFIG: (state, config) => {
      setOssConfig({
        endPonint: config.ossEndpoint,
        prefix: config.ossPrefix,
        bucket: config.ossBucket,
        region: config.ossRegion })

      state.endPonint = config.ossEndpoint
      state.prefix = config.ossPrefix
      state.bucket = config.ossBucket
      state.region = config.ossRegion
    }
  },

  actions: {
    // OSS设置
    OSSInit({ commit }) {
      return new Promise((resolve, reject) => {
        getOssConfig().then((data) => {
          commit('SET_CONFIG', data)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default ossconfig
